package com.naiterui.ehp.bs.consult.service.impl;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import com.google.common.collect.Lists;
import com.naiterui.ehp.bp.bo.consult.VideoConsultMessageBO;
import com.naiterui.ehp.bp.domain.VideoConsultMessage;
import com.naiterui.ehp.bp.support.exception.BusinessException;
import com.naiterui.ehp.bp.utils.beans.BeanUtil;
import com.naiterui.ehp.bs.consult.repository.VideoConsultMessageRepository;
import com.naiterui.ehp.bs.consult.service.IVideoConsultMessageService;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

/**
 * @author luao
 * @date 2021/2/27 4:32 下午
 **/
@Service
@Slf4j
@RequiredArgsConstructor
public class VideoConsultMessageServiceImpl implements IVideoConsultMessageService {

    private final VideoConsultMessageRepository videoConsultMessageRepository;

    @Override
    public VideoConsultMessageBO save(VideoConsultMessageBO videoConsultMessageBO) {
        // 初始化视频咨询消息
        VideoConsultMessage videoConsultMessage = new VideoConsultMessage();
        videoConsultMessage.setVideoConsultId(videoConsultMessageBO.getVideoConsultId());
        videoConsultMessage.setContent(videoConsultMessageBO.getContent());
        videoConsultMessage.setDoctorId(videoConsultMessageBO.getDoctorId());
        videoConsultMessage.setInquirerId(videoConsultMessageBO.getInquirerId());
        videoConsultMessage.setPatientId(videoConsultMessageBO.getPatientId());
        videoConsultMessage.setType(videoConsultMessageBO.getType());
        videoConsultMessage.setCreatedAt(new Date());
        // 添加视频咨询消息
        VideoConsultMessage save = videoConsultMessageRepository.save(videoConsultMessage);
        videoConsultMessageBO.setId(save.getId());
        return videoConsultMessageBO;
    }

    @Override
    public VideoConsultMessageBO getByRecomId(Long recomId) throws BusinessException {
        VideoConsultMessage message = videoConsultMessageRepository.findByContent(String.valueOf(recomId));
        VideoConsultMessageBO messageBO = new VideoConsultMessageBO();
        if (message != null) {
            BeanUtils.copyProperties(message, messageBO);
        }
        return messageBO;
    }

    @Override
    public List<VideoConsultMessageBO> getMessageByVideoConsultId(Long videoConsultId, List<Integer> types) {
        List<VideoConsultMessage> messages =
            this.videoConsultMessageRepository.findByVideoConsultIdAndTypeIn(videoConsultId, types);
        if (messages == null) {
            return Lists.newArrayList();
        }
        List<VideoConsultMessageBO> list = Lists.newArrayList();
        return BeanUtil.copyList(messages, list, VideoConsultMessageBO.class);
    }

    @Override
    public List<VideoConsultMessageBO> getVideoConsultMessage(List<String> contents, Integer type) {
        List<VideoConsultMessage> videoConsultMessages =
            videoConsultMessageRepository.findByContentInAndType(contents, type);
        if (CollectionUtils.isEmpty(videoConsultMessages)) {
            return null;
        }
        return videoConsultMessages.stream()
            .map(v -> VideoConsultMessageBO.builder().videoConsultId(v.getVideoConsultId()).type(v.getType())
                .inquirerId(v.getInquirerId()).patientId(v.getPatientId()).doctorId(v.getDoctorId())
                .content(v.getContent()).id(v.getId()).build())
            .collect(Collectors.toList());
    }
}
